//
//  SHKVCViewController.m
//  SDemo
//
//  Created by gg on 2018/8/6.
//  Copyright © 2018年 gg. All rights reserved.
//

#import "SHKVCViewController.h"
#import "SHBooks.h"
#import "SHCar.h"
#import "SHCar+test1.h"
#import "SHCar+test2.h"

@interface SHKVCViewController () {
    NSString *_location;
}

@property(nonatomic,copy)NSString *name;
@property(nonatomic,strong)SHCar *car;
@property(nonatomic,assign)int flag;

@end

@implementation SHKVCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"KVC";
    self.view.backgroundColor = [UIColor cyanColor];
    //[self sh_kvcget];
    
    //[self setApplied];
    
   // [self sh_changemodel];
}

-(void)sh_kvcget {
    [self setValue:@"kobe" forKey:@"name"];
    [self setValue:@"east" forKey:@"location"];
    NSLog(@"name : %@, location : %@",self.name, _location);
    
    SHCar *car = [[SHCar alloc] init];
    car.name = @"james";
    self.car = car;
    
    NSString *name1 = self.car.name;
    NSString *name2 = [self valueForKeyPath:@"car.name"];
    
    
    [self setValue:@"chris" forKeyPath:@"car.name"];
    
    name1 = self.car.name;
    name2 = [self  valueForKeyPath:@"car.name"];
    
    NSLog(@"name1 :%@, name2 :%@",name1,name2); //name1 :chris, name2 :chris
    
    //处理kvc 异常崩溃场景
    //下面的代码会崩溃
    [self setValue:nil forKey:@"flag"];
    NSLog(@"flag : %d",self.flag);
    
}

//几种常见的应用
-(void)setApplied {
    
    UITextField *textfile = [[UITextField alloc] init];
    textfile.frame = CGRectMake(20, 100, 150, 45);
    textfile.placeholder = @"请输入姓名";
    textfile.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:textfile];
    
    [textfile setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
    [textfile setValue:[UIFont systemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
    [textfile setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.backgroundColor"];
    
    //修改状态栏背景颜色
    [self setStatusBarBackgroundColor:[UIColor blueColor]];
    
    //2, 实现多层的消息传递
    NSArray *arr = @[@"john",@"tom",@"jim",@"lily"];
    NSArray *captinArr = [arr valueForKey:@"capitalizedString"];
    NSLog(@"captinArr :%@",captinArr);
    
    NSArray *lengthArr = [arr valueForKeyPath:@"capitalizedString.length"];
    NSLog(@"lengthArr : %@",lengthArr);
    
}


//模型转换
-(void)sh_changemodel {
    SHBooks *books = [[SHBooks alloc] init];
    books.name = @"something";
    books.author = @"adang";
    NSLog(@"author : %@",books);
    
    
    NSDictionary *dic = @{@"name":@"anything",
                          @"author":@"ssssss"};
    SHBooks *books2 = [SHBooks bookswithDic:dic];
    NSLog(@"author2 ： %@",books2);
    
    NSDictionary *dic3 = @{@"name":[NSNull null],
                          @"author":@"ssssss"};
    SHBooks *books3 = [SHBooks bookswithDic:dic3];
    NSLog(@"author3 ： %@",books3);
    
    
    
}

-(void)setStatusBarBackgroundColor:(UIColor *)color {
    UIView *statusbar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusbar respondsToSelector:@selector(setBackgroundColor:) ]) {
        [statusbar setValue:color forKey:@"backgroundColor"];
    }
}

//方法一： 重写setvalue方法
-(void)setValue:(id)value forUndefinedKey:(NSString *)key {
    NSLog(@"属性值不能为空");
}

//方法二： 重写setNilValueForKey

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
